import json
import globalData
import uuid


# 棋局
class ChessGame(object):
    def __init__(self,size):
        self.id = str(uuid.uuid4())
        self.p1 = None
        self.p2=None
        self.size = size
        self.board = Board(size)
        self.history = []
        # 上一个下棋的人的id
        self.last_p_id = None

    def addPlayer(self,p_id):
        player = Player(p_id,None)
        if self.p1 is  None:
            player.color=globalData.P1_COLOR
            self.p1 = player
            return
        player.color = globalData.P2_COLOR
        self.p2 = player


    def getBoard(self):
        return self.board.parseJsonStr()

    def paresJsonStr(self):
        p1_id = ''
        p2_id = ''
        if self.p1 is not None:
            p1_id = self.p1.id
        if self.p2 is not None:
            p2_id = self.p2.id

        return json.dumps({'chess_id':self.id,'p1_id':p1_id,'p2_id':p2_id,'size':self.size})

    def setCurrPlayer(self,p_id):
        self.last_p_id = p_id





# 棋盘
class Board(object):
    def __init__(self, size):
        self.size = size
        self.board = [[' '
        for _ in range(size)] for _ in range(size)]
    def save(self, row, col, color):
        if 0 <= row < self.size and 0 <= col < self.size and self.board[row][col] == ' ':
            self.board[row][col] = color
            return True
        return False

    def check_win(self, row, col, color):
        dicrory = [(1, 0), (0, 1), (1, 1), (1, -1)]
        for dr, dc in dicrory:
            count = 1
            for i in range(1, 5):
                r = row + dr * i  # dr=1
                c = col + dc * i  # dc=-0
                if 0 <= r < self.size and 0 <= c < self.size and self.board[r][c] == color:
                    count += 1
                else:
                    break
            for i in range(1, 5):
                r = row - dr * i  # dr=1
                c = col - dc * i  # dc=0
                if 0 <= r < self.size and 0 <= c < self.size and self.board[r][c] == color :
                    count += 1
                else:
                    break
            if count == 5:
                return True

        return False

    def back(self, row, col):
        self.board[row][col] = ' '

    def parseJsonStr(self):
        return json.dumps({"size":self.size,"board":json.dumps(self.board)})
# 玩家
class Player(object):
    def __init__(self, id, color):
        self.id = id
        self.color = color

    def play(self, board, row, col):
        return board.save(row, col, self.color)

# messageResponse类
class MessageResponse(object):
    def __init__(self,type,send_id,message):
        self.type = type
        self.send_id = send_id
        self.message = message

    def parseJsonStr(self):
        messageJson = ""
        if self.message is not None:
            if type(self.message) is str:
                messageJson = self.message
            else:
                messageJson = self.message.parseJsonStr()
        return json.dumps({'type':self.type,'send_id':self.send_id,'message':messageJson})

